/**
 * @author 王晓伟
 */
 /**
  * 初始化用户权限
  */
 var win,idtager='',sm, 
nametager='',nodetager = '',usersort= new Ext.data.Store(),userrolessort,fngroupid
function initshowuser(){
	
	 Ext.BLANK_IMAGE_URL=Ext.get('basePath').getValue()+"ajaxFramework/ext-2.1/resources/images/default/s.gif";
	 var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/departmenttree.do';
	 var load = new Ext.tree.TreeLoader({
	 	dataUrl:url
	 });
	 var root = new Ext.tree.AsyncTreeNode({
	 	id:'0',
	 	text:'天津市燃气集团有限公司'
	 })
	 var tree = new Ext.tree.TreePanel({
	 		x:5,
	 		y:5,	
        	autoScroll: true,
        	animate: true,
        	containerScroll: true,
        	border: true,
	        loader:load,	 
	        height:420,
	        width:230
	    });
	tree.setRootNode(root); 
	tree.on('click',function(node){
		usersort.load({
			params:{departmentid:node.id},
			callback:function(r,options,success){
				for(var i=0;i<r.length;i++)
				{
					var index = checkuserid.findBy(function(record,id){
						if(record.data.nodeid == node.id && record.data.userId ==  r[i].data.userId)
						{
							userchecksm.selectRecords([r[i]],true);
						}
					});
				}
			} 
		});
		checkoldnode=node.id;
	})
	
	/**
	 * 用户列表
	 */
	var userlisturl = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/selectUserByDepartmentList.do'
	var userrolesurl = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/fileDocument/selectUserRoles.do';
	usersort = new Ext.data.JsonStore({
		url:userlisturl,
		fields:['userId','uuid','userName','operate']
	});
   userrolessort = new Ext.data.JsonStore({
		url:userrolesurl,
		fields:['userId','uuid','userName']
	});
	var cm = new Ext.grid.ColumnModel([
		{header: "用户名", width: 75, dataIndex: 'userName',menuDisabled : true},
		{header:'操作',dataIndex:'operate',menuDisabled : true,renderer:function(s,m,rec){
		return "<a  href=javascript:adduser("+rec.data.userId+")>添加</a>";
		}},
		{header: "userId", width: 75, dataIndex: 'userId',menuDisabled : true,hidden:true},
		{header: "uuid", width: 75, dataIndex: 'uuid',menuDisabled : true,hidden:true}
	]);
	var grid = new Ext.grid.GridPanel({
		x:235,
	 	y:5,
		cm:cm,
		width:300,
		height:420,
		store:usersort,
		border: true,
	   	viewConfig:{
			forceFit:true	
		}
	});
	var userrolescm = new Ext.grid.ColumnModel([
		{header: "用户名", width: 75, dataIndex: 'userName',menuDisabled : true},
		{header: "userId", width: 75, dataIndex: 'userId',menuDisabled : true,hidden:true},
		{header: "uuid", width: 75, dataIndex: 'uuid',menuDisabled : true,hidden:true},
		{header:'操作',dataIndex:'operate',menuDisabled : true,renderer:function(s,m,rec){
		return "<a  href=javascript:deleteuser("+rec.data.userId+")>删除</a>";
		}}
	]);
	var userrolesgrid = new Ext.grid.GridPanel({
		x:535,
	 	y:5,
		cm:userrolescm,
		width:300,
		height:420,
		store:userrolessort,
		border: true,
	   	viewConfig:{
			forceFit:true	
		}
	})
	win = new Ext.Window({
		layout:'absolute',
		title:'设置下载权限',
		width:850,
		height:500,
		modal:true,
		closeAction:'hide',
		items:[tree,grid,userrolesgrid],
		buttonAlign:'right',
		buttons:[{
			text:'保存',
			handler:function(){
				var userid="",username="",nodeid="";
				userrolessort.each(function(record){
					userid+=record.data.userId+",";
				})
				submitrolse(fngroupid,userid);
				
			}
		},{
			text:'取消',
			handler:function(){
				win.hide();		
			}
		}]
		
	})
}

function userrolsewinshow(){
	win.show();
}

function selectuserrolse(groupid){
	userrolessort.removeAll();
	fngroupid = groupid;
	userrolessort.load({
		params:{groupid:groupid}
	});
}

function adduser(userid){
	if(userrolessort.find('userId',userid) == -1)
	{
		usersort.each(function(record){
			if(record.get('userId')==userid)
			{
				userrolessort.addSorted(record);
			}
		})		
	}
}

function deleteuser(userid){
	userrolessort.each(function(record){
		if(record.get('userId')==userid)
		{
			userrolessort.remove(record);
		}
	})
}
function submitrolse(groupid,userid){
	 Ext.MessageBox.show({
           title:'消息',
           msg: '是否执行此操作?',
           buttons: Ext.MessageBox.YESNO,
           fn: function(btn){
           	  if(btn == 'yes'){
           	  	    var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/fileDocument/updateUserRoles.do';
					Ext.Ajax.request({
						url:url,
						params:{
							groupid:groupid,
							userid:userid
						},
						success:function(response, options){
							    var responseArray = Ext.util.JSON.decode(response.responseText);                                              
			                     if(responseArray.success==true){
			                        Ext.Msg.alert('信息','操作成功！',function(){
			                        	win.hide();								
			                        });			                     
			                     }
			                    else{
			                    	Ext.Msg.alert('信息','操作失败！');     
			                    } 
						}
					});
           	  }	
           },
           icon: Ext.MessageBox.QUESTION
       });
}

